/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "GLWindow.h"
#include "ui_GLWindow.h"
#include "PlotableObject3D.h"
#include <QFileDialog>

GLWindow::GLWindow(PlotableObject3D* object,QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::GLWindow)
{
    m_ui->setupUi(this);
    m_ui->widget->setObject(object);
    connect(m_ui->saveButton,SIGNAL(clicked()),this,SLOT(onSaveClicked()));
}

GLWindow::~GLWindow()
{
    delete m_ui;
}

void GLWindow::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void GLWindow::onSaveClicked(){
    QSize size = m_ui->widget->size();
    if(size.isValid()){
        QImage image = m_ui->widget->grabFrameBuffer();
        QString fileName = QDir::homePath()+"/Unnamed.jpg";
        QString selectedFilter = "JPG(*.jpg)";
        fileName = QFileDialog::getSaveFileName(this,
                                                tr("Save image as..."),
                                                fileName,
                                                tr("BMP(*.bmp);;JPG(*.jpg);;PNG(*.png);;XPM(*.xpm)"),
                                                &selectedFilter);
        QString format =selectedFilter.mid(0,3).toUpper();
        image.save(fileName,format.toAscii().data(),100);
    }
}
